package config

import (
	"fmt"
	"github.com/go-ini/ini"
)

type Config struct {
	Log           LogConfig  `ini:"log"`
	HttpConf      HttpConfig `ini:"http_conf"`
	PprofProvider PprofConf  `ini:"pprof"`
	Server        ServerConf `ini:"server"`
	DaoConf       DaoConfig  `ini:"dao"`
	PPolicy       PriPolicy  `ini:"pri_policy"`
	Jwt           JwtConfig  `ini:"jwt"`
	NsqProvider   NsqConfig  `ini:"nsq"`
	MailConf      MailConfig `ini:"mail"`
}

type LogConfig struct {
	LogFile string `ini:"log_file"`
}

type MailConfig struct {
	Host    string `ini:"host"`
	Sender  string `ini:"sender"`
	Pwd     string `ini:"pwd"`
	Subject string `ini:"subject"`
}

type DaoConfig struct {
	RedisConfig `ini:"redis"`
	MysqlConfig `ini:"mysql"`
}

type JwtConfig struct {
	JwtSecret string `ini:"jwt_secret"`
	JwtIssue  string `ini:"jwt_issue"`
}

type MysqlConfig struct {
	User    string `ini:"user"`
	Pass    string `ini:"pass"`
	Host    string `ini:"host"`
	Port    int    `ini:"port"`
	Name    string `ini:"name"`
	Socket  string `ini:"socket"`
	Debug   bool   `ini:"debug"`
	Maxidle int    `ini:"maxidle"`
	Maxconn int    `ini:"maxconn"`
	Charset string `ini:"charset"`
}

type RedisConfig struct {
	Addr string `ini:"addr"`
	Pwd  string `ini:"pwd"`
	DB   int    `ini:"db"`
	TTL  string `ini:"ttl"` //24h
}

type NsqConfig struct {
	Addr          string   `ini:"addr"`
	ConTopic      string   `ini:"con_topic"`
	ProTopic      string   `ini:"pro_topic"`
	ConChannel    string   `ini:"con_channel"`
	NsqLookUpAddr []string `ini:"nsq_lookup_addr"`
}

type PprofConf struct {
	Enable bool   `ini:"enable"`
	Addr   string `ini:"addr"`
}

type ServerConf struct {
}

type HttpConfig struct {
	ServiceConf ServiceConf  `ini:"http_conf.server_conf"`
	ClientConf  ClientConfig `ini:"http_conf.client_conf"`
}

type ServiceConf struct {
	TlsAddr string `ini:"tls_addr"`
	Enable  bool   `ini:"enable"`
}

type ClientConfig struct {
	PingDuration string `ini:"ping_duration"`

	PingUrl string `ini:"ping_url"`
}

type PriPolicy struct {
	RegularUser RegularUserMethod `ini:"pri_policy.regular_user"`
	Admin       AdminMethod       `ini:"pri_policy.admin"`
	SuperAdmin  SuperAdminMethod  `ini:"pri_policy.super_admin"`
}

type RegularUserMethod struct {
	Post string `ini:"post"`
	Get  string `ini:"get"`
}

type AdminMethod struct {
	Post string `ini:"post"`
	Get  string `ini:"get"`
}

type SuperAdminMethod struct {
	Post string `ini:"post"`
	Get  string `ini:"get"`
}

// NewConfig initialize the configuration instance
func NewConfig() *Config {
	var err error
	conf := new(Config)
	err = ini.MapTo(conf, "app.ini")
	if err != nil {
		fmt.Printf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
	}
	fmt.Printf("%+v\n", conf)
	return conf
}
